package com.woniuxy.controller;

import com.woniuxy.entity.Production;
import com.woniuxy.entity.QueryEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ProductionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author Leimin
 * @date 2022/04/26 19:09
 **/
@RestController
@RequestMapping("/production")
@Api(tags = "产品接口")
public class ProductionController {
    @Autowired
    ProductionService productionService;

    @PostMapping("/condition")
    @ApiOperation(value = "条件查询产品分页数据")
    public ResponseEntity findAllByPage(@ApiParam(name = "queryEntity", value="查询条件") @RequestBody QueryEntity<Production> queryEntity){
        return productionService.findAllByCondition(queryEntity);
    }

    @GetMapping("/production")
    @ApiOperation(value = "获取所有产品数据")
    public ResponseEntity findAll(){

        return productionService.findAll();
    }

    @GetMapping("/production/{id}")
    @ApiOperation(value = "获取一个产品数据")
    public ResponseEntity findOne(@ApiParam(name = "id", value="产品id") @PathVariable int id){

        return productionService.findOne(id);
    }

    @PostMapping("/production")
    @ApiOperation(value = "新增产品")
    public ResponseEntity addOne(@ApiParam(name = "production", value="新增的产品") @RequestBody Production production){

        return productionService.addOne(production);
    }

    @DeleteMapping("/production/{id}")
    @ApiOperation(value = "删除产品")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value="产品id") @PathVariable int id){

        return productionService.deleteOne(id);
    }

    @PutMapping("/production/batchDel")
    @ApiOperation(value = "批量删除产品")
    public ResponseEntity deleteBatch(@ApiParam(name = "ids", value="产品id数组")@RequestBody int[] ids){
        System.out.println(ids);
        return productionService.deleteBatch(ids);
    }

    @PutMapping("/production")
    @ApiOperation(value = "更新产品")
    public ResponseEntity updateOne(@ApiParam(name = "production", value="新增的产品") @RequestBody Production production){

        return productionService.updateOne(production);
    }

    @GetMapping("/select")
    @ApiOperation(value = "获取下拉框数据")
    public ResponseEntity findSelectOption(){
        return productionService.findSelectOption();
    }

    @GetMapping("/code/{code}")
    @ApiOperation(value = "根据编码查询产品数据")
    public ResponseEntity findByCode(@ApiParam(name = "code", value="产品编码") @PathVariable String code){

        return productionService.findByCode(code);
    }

    @GetMapping("/name/{name}")
    @ApiOperation(value = "根据名称查询产品数据")
    public ResponseEntity findByName(@ApiParam(name = "name", value="产品名称")@PathVariable String name){
        String newName=null;
        try {
            newName= URLDecoder.decode(name,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return productionService.findByName(newName);
    }
}


